package com.hsy.crm.web.web.system;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Customer;
import com.hsy.crm.web.domain.vo.customer.CustomerPageVo;
import com.hsy.crm.web.service.base.CacheService;
import com.hsy.crm.web.service.impl.CustomerService;
import com.hsy.crm.web.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/customer")
public class CustomerAction extends BaseAction {

    @Autowired
    CustomerService customerService;

    @RequestMapping("/insert")
    public String insert(Customer customer) {
        Unified<Integer> unified = customerService.insertCustomer(customer);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.putCustomerId(customer.getCustomerId(),customer.getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/update")
    public String update(Customer customer) {
        return Response.body(customerService.updateCustomer(customer));
    }

    @RequestMapping("/delete")
    public String delete(Customer customer) {
        Unified<Integer> unified = customerService.deleteCustomer(customer);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.removeCustomerId(customer.getCustomerId(),customer.getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/get")
    public String get(Customer customer) {
        return Response.body(customerService.getCustomer(customer));
    }

    @RequestMapping("/page")
    public String page(CustomerPageVo customerPageVo) {
        return Response.body(customerService.page(customerPageVo));
    }

}
